﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Habito.Helper;
using Newtonsoft.Json;
using Common;

namespace Habito.Admini
{
	public partial class ProductClass : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(Ticket.UserName) || Session["username"] == null)
			{
				Response.Redirect("Login.aspx");
			}
			ViewState["classData"] = DataHelper.GetAllProductClass();
			ViewState["FirstLevelClass"] = DataHelper.GetProductFirstClass();
		}
		protected override void OnInit(EventArgs e)
		{
			if (Request.QueryString["AddProductClass"] == "1")
			{
				int id = int.Parse(Request.QueryString["id"] ?? "0");
				string name = Request.QueryString["name"] ?? "";
				int pid = Convert.ToInt32(Request.QueryString["pid"] == "" ? "0" : Request.QueryString["pid"]);
				Ajax.Model.ProductClass newclass = new Ajax.Model.ProductClass();
				Ajax.BLL.ProductClass bll = new Ajax.BLL.ProductClass();
				if (id != 0)
				{
					newclass.ID = id;
				}
				newclass.Name = name;
				if (pid != 0)
				{
					newclass.PID = pid;
				}
				if (id != 0)
				{
					bll.Update(newclass);
					// modify
				}
				else
				{
					bll.Add(newclass);
					newclass = bll.GetModel(bll.GetMaxId());
					//insert
				}
				Response.Write(JsonConvert.SerializeObject(newclass));
				Response.End();
			}
			if (Request.QueryString["GetAllClass"] != null)
			{
				String temp = DataHelper.GetAll();
				Response.Write(temp);
				Response.End();
			}

			if (Request.QueryString["DeleteProductClass"] == "1")
			{
				int id = int.Parse(Request.QueryString["id"].Replace("\'", ""));
				Ajax.BLL.ProductClass c = new Ajax.BLL.ProductClass();
				bool result = c.Delete(id);
				Response.Write(result.ToString());
				Response.End();
			}

			base.OnInit(e);
		}
	}
}